package com.meixiang.view.statusView;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;

import com.meixiang.inteface.IattachViewListener;
import com.meixiang.tool.Tool;

/**
 * Activity状态布局（无数据、无网络、正在加载中...）
 * author:xiejingwen
 */
public class ActivityStatusView extends StatusView {
    private Activity activity;
    private View titleView;
    private Context mContext;

    public ActivityStatusView(Context context, Activity activity) {
        super(context);
        mContext = context;
        this.activity = activity;

    }

    /**
     * 设置titleView，使得布局显示在该view之下(不设置则显示全屏)
     *
     * @param titleView
     */
    public void setTitleView(View titleView) {
        this.titleView = titleView;
    }

    /**
     * 显示无网络状态（有title则显示title之下，无title显示全屏）
     */
    public void showNoNewWork(final OnClickListener listener){
        removeView();
        setAboveView(titleView, new IattachViewListener() {
            @Override
            public void onSettingComplete() {
                NoNewWork(listener);
            }
        });
    }

    /**
     * 显示无网络状态(显示在传入view的范围)
     *
     * @param view 传入需被覆盖的View
     */
    public void showNoNewWork(View view, final OnClickListener listener) {
        setCoverageView(view, new IattachViewListener() {
            @Override
            public void onSettingComplete() {
                NoNewWork(listener);
            }
        });
    }

    /**
     * 显示无数据状态，有点击事件(显示在title之下，无title显示全屏)
     *
     * @param message  显示的内容
     * @param btnText  按钮文字
     * @param listener 点击事件
     */
    public void showNoData(final String message, final String btnText, final OnClickListener listener) {
        setAboveView(titleView, new IattachViewListener() {
            @Override
            public void onSettingComplete() {
                NoData(message, btnText, listener);
            }
        });
    }

    /**
     * 显示无数据状态,有点击事件(显示在传入view的范围)
     *
     * @param view     传入需被覆盖的View
     * @param message  显示的内容
     * @param btnText  按钮文字
     * @param listener 点击事件
     */
    public void showNoData(View view, final String message, final String btnText, final OnClickListener listener) {
        setCoverageView(view, new IattachViewListener() {
            @Override
            public void onSettingComplete() {
                NoData(message, btnText, listener);
            }
        });
    }

    /**
     * 显示无数据状态 无点击事件(有title则显示title之下，无title显示全屏)
     *
     * @param message 显示的内容
     */
    public void showNoData(final String message) {
        setAboveView(titleView, new IattachViewListener() {
            @Override
            public void onSettingComplete() {
                NoData(message);
            }
        });
    }

    /**
     * 显示无数据状态,无点击事件(显示在传入view的范围)
     *
     * @param view    传入需被覆盖的View
     * @param message 显示的内容
     */
    public void showNoData(View view, final String message) {
        setCoverageView(view, new IattachViewListener() {
            @Override
            public void onSettingComplete() {
                NoData(message);
            }
        });
    }

    /**
     * 显示正在加载中状态（有title则显示title之下，无title显示全屏）
     */
    public void showLoading() {
        setAboveView(titleView, new IattachViewListener() {
            @Override
            public void onSettingComplete() {
                Loading();
            }
        });

    }

    /**
     * 显示正在加载中状态(显示在传入view的范围)
     *
     * @param view 传入需被覆盖的View
     */
    public void showLoading(View view) {
        setCoverageView(view, new IattachViewListener() {
            @Override
            public void onSettingComplete() {
                Loading();
            }
        });

    }

    /**
     * 初始化布局，将附加布局添加到传入View的下面（如：传入title)
     */
    private void setAboveView(final View aboveView, final IattachViewListener listener) {
        final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        if (aboveView == null) {
            params.topMargin = 0;
            removeAttachView();
            activity.addContentView(this, params);
            listener.onSettingComplete();
        } else {
            if (aboveView.getWidth() == 0) {
                aboveView.post(new Runnable() {
                    @Override
                    public void run() {
                        params.topMargin = aboveView.getHeight();
                        removeAttachView();
                        activity.addContentView(ActivityStatusView.this, params);
                        listener.onSettingComplete();
                    }
                });
            } else {
                params.topMargin = aboveView.getHeight();
                removeAttachView();
                activity.addContentView(this, params);
                listener.onSettingComplete();
            }


        }
    }

    /**
     * 初始化布局，将附加布局覆盖在传入的View的范围
     *
     * @param coverageView 被覆盖的布局
     * @param listener     设置完成的监听（做显示加载中、无数据、无网络等布局的操作）
     */
    private void setCoverageView(final View coverageView, final IattachViewListener listener) {
        if (coverageView == null) return;
        if (coverageView.getWidth() == 0) {
            coverageView.post(new Runnable() {
                @Override
                public void run() {
                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(coverageView.getWidth(), coverageView.getHeight());
                    int[] position = new int[2];
                    coverageView.getLocationInWindow(position);
                    params.setMargins(position[0], position[1] - Tool.getStatusBarHeight(mContext), Tool.getWindowWith(mContext) - coverageView.getRight(), Tool.getWindowHeight(mContext) - coverageView.getBottom());
                    removeAttachView();
                    activity.addContentView(ActivityStatusView.this, params);
                    listener.onSettingComplete();
                }
            });
        } else {
            int[] position = new int[2];
            coverageView.getLocationInWindow(position);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(coverageView.getWidth(), coverageView.getHeight());
            params.setMargins(position[0], position[1] - Tool.getStatusBarHeight(mContext), Tool.getWindowWith(mContext) - coverageView.getRight(), Tool.getWindowHeight(mContext) - coverageView.getBottom());
            removeAttachView();
            activity.addContentView(this, params);
            listener.onSettingComplete();
        }

    }

    /**
     * 删除布局（请求成功或网络连接正常后调用）
     */
    public void removeView() {
        removeAttachView();
    }

}
